package com.jrsoft.bim;

import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;

/**
 * ${DESCRIPTION}
 *
 * @author Blueeyedboy
 * @create 2019-12-20 6:53 PM
 **/
public class ApplicationWebViewRunner extends Application{
	private static final String DEFAULT_URL = "http://localhost:";

	private static final int MIN_HEIGHT = 1000;
	private static final int MIN_Width = 1366;

	public static String port;
	public static boolean launch;



	@Override
	public void start(Stage stage) {
		init(stage);
		stage.show();
	}

	private void init(Stage stage) {
		stage.setMinHeight(MIN_HEIGHT);
		stage.setMinWidth(MIN_Width);



		stage.setResizable(true);
		stage.setOnCloseRequest(event -> System.exit(0));

		Group group = new Group();
		stage.setScene(new Scene(group));
		stage.setMaximized(true);
//		stage.setFullScreen(true);
		stage.setIconified(true);
		WebView webView = new WebView();
		webView.setMinHeight(1000);
		webView.setMinWidth(1366);
		final WebEngine engine = webView.getEngine();
		System.out.println(DEFAULT_URL+"-----------"+port);
		engine.load(StringUtils.join(DEFAULT_URL, port,"/v2"));

		final TextField textField = new TextField(StringUtils.join(DEFAULT_URL, port));

		engine.locationProperty().addListener((observable, oldValue, newValue) -> textField.setText(newValue));

		engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
			if (newValue == Worker.State.SUCCEEDED) {
				stage.setTitle(engine.getTitle());
			}
		});

		VBox vBox = new VBox();
		vBox.getChildren().addAll(webView);
		VBox.setVgrow(webView, Priority.NEVER);

		group.getChildren().add(vBox);
	}


}
